<?php
// +----------------------------------------------------------------------
// |优惠券管理
// +----------------------------------------------------------------------
// |Data:2016-06-25 16:55
// +----------------------------------------------------------------------
// |Author: 吕志雄 <870923001@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Common\Controller\AdminCommonController;

class CouponsController extends AdminCommonController{
    private $_model;
    protected function _initialize()
    {
        parent::_initialize();
        $this->_model = D('Common/Coupons');
    }

    /**
     * 列表
     */
    public function index(){
        $map = array();
        $map['is_del'] = 2;
        if($_SESSION['administrator']){
            $map['id'] = array('gt',0);
        }else{
            $map['uid'] = $this->uid;
        }
        $list = $this->_model->getList($map,'add_time desc');
        foreach($list['data'] as &$val){
            $detail = D('Common/Shop')->detail($val['shop_id']);
            $val['shopTitle'] = $detail['title'];
        }
        $this->assign($list);
        $this->display();
    }

    /**
     * 添加、编辑页面显示
     */
    public function edit(){
        $id = I('get.id');
        if($id){
            $detail = $this->_model->detail($id);
            $detail['ext_time'] = toDate($detail['ext_time'],'Y-m-d');
            $detail['publish_time'] = toDate($detail['publish_time'],'Y-m-d');
            $this->assign('detail',$detail);
        }
        $shopList = D('Common/Shop')->getShopList($this->manage_id);
        $this->assign('shopList',$shopList);
        $this->display();
    }

    /**
     * 更新、添加
     */
    public function update(){
        $data = $this->checkPostData();
        empty($data) || !is_array($data) && $this->returnData('请填写表单数据！');
        $result = $this->_model->update($data);
        $msg = $data['id'] ? '编辑' : '添加';
        if($result){
            $this->addHistoryLog($msg.'优惠券成功');
            $this->returnData($msg.'优惠成功！',1,'/Coupons/index');
        }else{
            $this->returnData($msg.'优惠券失败！');
        }
    }

    /**
     * 删除
     */
    public function del(){
        $id = I('get.id');
        empty($id) && $this->returnData('请选择操作项！');
        $detail = $this->_model->detail($id);
        empty($detail) && $this->returnData('该记录不存在！');
        $is_del = $detail['is_del'] == 1 ? 2 : 1;
        $data = array(
            'id'=>$id,
            'is_del'=>$is_del
        );
        $result = $this->_model->update($data);
        if($result){
            $this->addHistoryLog('删除优惠券成功');
            $this->returnData('删除成功！',1);
        }else{
            $this->returnData('删除失败！');
        }
    }

    /**
     * 领取记录
     * @return bool
     */
    public function record(){
        $id = I('get.id');
        if(empty($id)) return false;

        $map = array();
        $map['coupons_id'] = $id;
        $list = $this->_model->getRecordList($map);
        $this->assign($list);
        $this->assign('id',$id);
        $this->display();
    }

    /**
     * 检测表单数据
     * @return array
     */
    private function checkPostData(){
        $id = I('post.id');
        $shop_id = I('post.shop_id');
        $title = I('post.title');
        $denom = I('post.denom');
        $max_decom = I('post.max_decom');
        $store = I('post.store');
        $publish_time = I('post.publish_time');
        $ext_time = I('post.ext_time');
        $desc = I('post.desc');

        empty($shop_id) && $this->returnData('请选择店铺！');
        if($_SESSION['administrator']){
            $detail = D('Common/Shop')->detail($shop_id);
            $uid = $detail['uid'];
        }else{
            $uid = $this->manage_id;
        }

        empty($title) && $this->returnData('请输入优惠券名称！');
        if(abslength($title) > 30) $this->returnData('优惠券长度1~30个字符！');
        empty($denom) && $this->returnData('请输入优惠券面额！');
        if(!moneyRegx($denom)) $this->returnData('面额格式错误！');
        empty($max_decom) && $this->returnData('请输入最大面额！');
        if(!moneyRegx($max_decom)) $this->returnData('最大额度格式错误！');
        empty($store) && $this->returnData('请输入优惠券数量！');
        if(!checkRegx("/[0-9]*/",$store)) $this->returnData('优惠券数量格式错误！');
        empty($publish_time) && $this->returnData('请输入领取日期！');
        empty($ext_time) && $this->returnData('请输入使用日期！');
        $publish_time = strtotime($publish_time);
        $ext_time = strtotime($ext_time);
        if($publish_time > $ext_time) $this->returnData('使用日期必须大于领取日期');
        if($desc && abslength($desc) > 100) $this->returnData('描述长度1~100个字符！');

        $data = array(
            'title'=>$title,
            'uid'=>$uid,
            'shop_id'=>$shop_id,
            'denom'=>$denom,
            'max_decom'=>$max_decom,
            'desc'=>$desc,
            'store'=>$store,
            'ext_time'=>getMkTime($ext_time),
            'publish_time'=>getMkTime($publish_time),
            'is_del'=>2,
        );
        if($id){
            $data['id'] = $id;
            $data['edit_time'] = time();
        }else{
            $data['add_time'] = time();
        }
        return $data;
    }
}
?>